$(function(){
  //禁止返回
    history.pushState(null, null, document.URL);
    window.addEventListener('popstate', function () {
        history.pushState(null, null, document.URL);
    });

//进入抽奖页埋点
window.Interface.tracking('活动详情页', '幸运抽奖', '')
 
$("a").on("click",function(e){
  //e.preventDefault();
  if($(this).hasClass("my-prize")){
    window.Interface.toMyPrize();
  }else{
    window.location.href= $(this).attr("rel");
  }
});

window.Interface.luckyNum({},function(res){
  $("#lucky-num").text(res.luckyNum);

if(parseInt(res.luckyNum)<=0){
    $("#award-4").show();
}


  if(parseInt(res.luckyNum)>=1 && window.DeviceMotionEvent){
    //摇一摇
    window.addEventListener('devicemotion', deviceMotionHandler, false);
  }


});


var open = true;

var SHAKE_THRESHOLD = 3000;  
var last_update = 0;  
var x = y = z = last_x = last_y = last_z = 0;  
function deviceMotionHandler(eventData) {  

  var acceleration = eventData.accelerationIncludingGravity;  
  var curTime = new Date().getTime();  
  if ((curTime - last_update) > 100) {  
      var diffTime = curTime - last_update;  
      last_update = curTime;  
      x = acceleration.x;  
      y = acceleration.y;  
      z = acceleration.z;  
      var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;  
      if (speed > SHAKE_THRESHOLD) {  
        //注视掉部分为判断震动
//         if ((navigator.vibrate || navigator.webkitVibrate) && window.webkitNotifications) {     
// //          if(window.webkitNotifications.checkPermission() != 0) {
// //            window.webkitNotifications.requestPermission(function () {});
// //          }
//           if (navigator.vibrate) {
//             navigator.vibrate(2000);
//           }else if(navigator.webkitVibrate){
//             navigator.webkitVibrate(2000);
//           }
//         }

        //随机一个数字，显示弹出框
        // var pi = parseInt(randomInteger(1,4));
        // $("#award-"+pi).fadeIn().siblings(".pop-box-challenger").hide();
        if(open){
          open = false;
          window.Interface.tracking('幸运抽奖', '摇一摇','');
          if(parseInt($("#lucky-num").text()) > 0){
            window.Interface.luckyNumRes({},function(res){
              $("#lucky-num").text(res.luckyNum);
              if(res.isPrize){
                $("#award-"+res.luckyRes).fadeIn().siblings(".pop-box-challenger").hide();
                if(res.luckyRes === 2){
                  $(".tc").text(res.prizeFee);
                }
              }else{
                $("#award-3").show();
              }
            });
          }else{
            $("#award-4").show();
          }
        }
      }  
      last_x = x;  
      last_y = y;  
      last_z = z;  
  }  
}


$(".pop-box-challenger .close").on("click",function(){
  $(this).parents(".pop-box-challenger").fadeOut();
  open = true;
});

$("#award-3 .btn-green").on("click",function(){
  $(this).parents(".pop-box-challenger").fadeOut();
  open = true;
});
$("#award-3 .go-prize").on("click",function(){
  //去领奖
   window.Interface.toMyPrize();
});
$("#award-1 .go-prize").on("click",function(){
  //去领奖
   window.Interface.toMyPrize();
});


});

function randomInteger(min, max){
    return min + Math.floor(Math.random() * (max - min));
}

